package com.wfs.utils;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;

import static com.wfs.utils.Common.*;


/**
 * @author: wfs
 * @Date: 2021/1/13 10:28
 * @Description:工具翻译
 */
public class BasicCacheHelperNew {
   static Map<Long,String> userCache=new HashMap<>();
    static  {
        userCache.put(1L,"user1");
        userCache.put(2L,"user2");

    }



    public <Data> Stream<Data> stream(Collection<Data> list) {
        return new Stream<>(list);
    }

    public class Stream<Data> {
        private Collection<Data> datas;

        Stream(Collection<Data> datas) {
            this.datas = datas;
        }
        private List<Translator<Data>> translators = new ArrayList<>();
        /**
         * id 转换名称
         */
        public Stream<Data> userIdToName(Function<Data, Long> getId, BiConsumer<Data, String> setName) {
            translate(Common.ID_TO_NAME,getId,setName);
            return this;
        }


        private Stream<Data> translate(int type, Function getId, BiConsumer setName) {
            if (Common.isTranslateType(type)) {
                translators.add(new Translator(type, getId, setName));
            }
            return this;
        }

        /**
         * 执行前面预定的所有翻译动作，并使用缓存和批量查询功能
         */
        public void translate() {
            try {
                NoCacheData<Data> noCacheData = new NoCacheData();
                for (Data datum : datas) {
                    for (Translator translator : translators) {
                        translate(noCacheData, datum, translator);
                    }
                }
               /* batchByUserId(noCacheData);
                batchByCustomerId(noCacheData);
                batchByUserCode(noCacheData);*/
                // 再次执行翻译动作
             /*   for (Data datum : noCacheData.againDataSet) {
                    for (Translator<Data> translator : noCacheData.translatorSet) {
                        translate(null, datum, translator);
                    }
                }*/
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        private void translate(NoCacheData<Data> noCacheData, Data datum, Translator translator) {
            Object key = translator.get.apply(datum);
            if (key != null) {
                Object val = userCache.get( key);
                if (val == null && noCacheData != null) {
                    // 没有缓存则添加到 noCacheData，后续批量查询处理
                    if (key instanceof String && isCodeTranslateType(translator.type)) {
                        noCacheData.add((String) key).add(translator).addData(datum);
                    } else if (key instanceof Long && isIdTranslateType(translator.type)) {
                        noCacheData.add((Long) key).add(translator).addData(datum);
                    }
                }
                // 当缓存内容是null或者NULL时不用设置
                if (!(val == null || Objects.equals(val, NULL))) {
                    translator.set.accept(datum, val);
                }
            }
        }
    }

    private class NoCacheData<Data> {
        HashSet<Long> userIds = new HashSet<>();
        HashSet<Long> customerIds = new HashSet<>();
        HashSet<String> userCodes = new HashSet<>();
        HashSet<Data> againDataSet = new HashSet<>();
        HashSet<Translator<Data>> translatorSet = new HashSet<>();

        NoCacheData add(Long id) {

            return this;
        }

        NoCacheData add(String code) {
            userCodes.add(code);
            return this;
        }

        NoCacheData addData(Data datum) {
            againDataSet.add(datum);
            return this;
        }

        NoCacheData add(Translator item) {
            translatorSet.add(item);
            return this;
        }
    }



}
